package com.hongyang.servlet.baby;

import com.alibaba.fastjson.JSONObject;
import com.hongyang.dto.BabyDTO;
import com.hongyang.service.BabyService;
import com.hongyang.service.impl.BabyServiceImpl;
import com.hongyang.vo.BootstrapTableVO;
import com.hongyang.vo.ResultVO;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@WebServlet(urlPatterns = "/hr/newbaby/getnewbabydanganlist")
public class Search extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        //设置编码格式
        req.getParameter("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        //接受查询参数
        BabyDTO babyDTO=new BabyDTO();
        //当前页数
        String pageNumber = req.getParameter("pageNumber");
        if (StringUtils.isNotBlank(pageNumber)){
            babyDTO.setPageNumber(Integer.parseInt(req.getParameter("pageNumber")));
        }
        //每页显示数据个数
        String pageSize = req.getParameter("pageSize");
        if (StringUtils.isNotBlank(pageSize)){
            babyDTO.setPageSize(Integer.parseInt(req.getParameter("pageSize")));
        }

        //出生证号
        String birthNum = req.getParameter("birthNum");
        if (StringUtils.isNotBlank(birthNum)){
            babyDTO.setBirthNum(req.getParameter("birthNum"));
        }

        //姓名
        String babyName = req.getParameter("babyName");
        if (StringUtils.isNotBlank(babyName)){
            babyDTO.setBabyName(req.getParameter("babyName"));
        }

        //设置日期格式
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        //登记开始日期
        String registerDateMIN = req.getParameter("registerDateMIN");
        //登记结束日期
        String registerDateMAX = req.getParameter("registerDateMAX");
        if (StringUtils.isNotBlank(registerDateMIN) && StringUtils.isNotBlank(registerDateMAX)){
            try {
                babyDTO.setRegisterDateMIN(sdf.parse(req.getParameter("registerDateMIN")));
                babyDTO.setRegisterDateMAX(sdf.parse(req.getParameter("registerDateMAX")));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        BabyService service=new BabyServiceImpl();
        BootstrapTableVO search = service.search(babyDTO);

        ResultVO success = ResultVO.success(search);

        PrintWriter writer = res.getWriter();
        String json= JSONObject.toJSONString(success);
        writer.println(json);
        writer.flush();
        writer.close();


    }
}
